package com.small.act;

import java.util.HashMap;
import java.util.List;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ListView;

import com.small.adapter.FavDiscountAdapter;
import com.small.commom.Action;
import com.small.commom.AsyncDataLoader;
import com.small.commom.AsyncDataLoader.Callback;
import com.small.model.FavDiscountModel;
import com.smell.R;

public class FavDiscountActivity extends Activity{

	private ListView list_view;

	private ImageButton btn_back;
	private Button btn_editfav;

	private Action action;
	private int result;

	private HashMap<String, Object> favDiscountMap = null;
	List<FavDiscountModel> discountList = null;
	FavDiscountAdapter list_adapter; 

	int intPage = 1;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_favdiscount_view);
		action = new Action(FavDiscountActivity.this);
		findView();
		initData();
	}

	public void findView() {
		btn_editfav = (Button)this.findViewById(R.id.btn_editfav);
		btn_editfav.setOnClickListener(onClick);
		btn_back = (ImageButton) this.findViewById(R.id.btn_back);
		btn_back.setOnClickListener(onClick);
		list_view = (ListView) this.findViewById(R.id.mylook_list);
		list_view.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View view, int position,long arg3) {
						startActivity(new Intent(FavDiscountActivity.this,DiscountDetailActivity.class));
			}
		});
	}

	private void initData() {
		action.showProgressDialog("正在加载...");
		new AsyncDataLoader(asyncCallback).execute();
	}

	private Callback asyncCallback = new AsyncDataLoader.Callback() {

		@Override
		public void onStart() {
//			favDiscountMap = action.getFavDiscountInfo(Common.model.getMemberID(), 2, 1);
			favDiscountMap = action.getFavDiscountInfo("1", 5, 1);
		}

		@Override
		public void onPrepare() {
		}

		@SuppressWarnings("unchecked")
		@Override
		public void onFinish() { 

			action.stopProgressDialog();
			 
			
			if (favDiscountMap != null) {
				Integer i = (Integer) favDiscountMap.get("result");
				result = (int) i;
				discountList = (List<FavDiscountModel>) favDiscountMap.get("discountList");

				resultMethod(result, discountList);				
			}  else{
				resultMethod(-2,null);
			}
			
		}
	};

	OnClickListener onClick = new OnClickListener() {

		@Override
		public void onClick(View v) {

			switch (v.getId()) {
			case R.id.btn_back:// 返回
				finish();
				break;
			case R.id.btn_editfav:// 编辑收藏优惠券 
				if (!list_adapter.getEdit()) {
					btn_editfav.setText("完成编辑");
					list_adapter.setEdit(true);
				}else{
					btn_editfav.setText("编辑 ");
					list_adapter.setEdit(false);
				}
				
				break; 
			}
		}
	};

	protected void resultMethod(int result2, List<FavDiscountModel> mydiscountlist) {
		switch (result2) {
		case 0:
			action.showShortToast("请求失败，请稍候再试...");
			break;
		case 1:
			if (mydiscountlist != null) {
				list_adapter = new FavDiscountAdapter(FavDiscountActivity.this,mydiscountlist);
				list_view.setAdapter(list_adapter);
			} else {
				action.showShortToast("请求失败，请稍候再试...");
			}
			break;
		case -2:
			action.showShortToast("数据异常，请求失败...");
			break;

		default:
			break;
		}
	}

	 
}
